#include "qCenovnikwidget.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlError>
#include <QDesktopWidget>
#include "chelperclass.h"

QCenovnikWidget::QCenovnikWidget(QWidget *parent)
	: QMyBaseFormWidget(parent)
    ,m_selectedText("") 
    ,m_selectedTextName("")
{
	ui.setupUi(this);
	QDesktopWidget desk;
	QRect deskRect = desk.screenGeometry();
	ui.layoutWidget->setFixedWidth(deskRect.width() - 300);
	ui.layoutWidget->setFixedHeight(deskRect.height()-200);
    ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui.tableView->setSortingEnabled(true);
	connect(ui.tableView->horizontalHeader(),SIGNAL(sectionClicked(int)), ui.tableView, SLOT(sortByColumn(int)));
	connect(ui.comboBox, SIGNAL(currentIndexChanged(const QString &)),this, SLOT(procComboBoxChanged(const QString &)));
    createProdavnicaList();
}

QCenovnikWidget::~QCenovnikWidget()
{
}

void QCenovnikWidget::radioButtonClicked()
{
}

void QCenovnikWidget::pressEscape()
{
    emit closeW();
}
void QCenovnikWidget::procComboBoxChanged(const QString & text)
{
	int stop = 0;
	QStringList mailids = text.split("-");
	lista(mailids.at(0));
}
void QCenovnikWidget::createProdavnicaList()
{
	QString temp = 
		" SELECT  distinct komintenti.id, komintenti.naziv "
		" FROM magacin_prodavnica_cenovnik AS cenovnik "
		" LEFT JOIN artikli ON cenovnik.artikal_id = artikli.id "
		" LEFT JOIN komintenti ON cenovnik.prodavnica_id = komintenti.id ";
	QSqlQuery query(temp);
	ui.comboBox->addItem("");
	while (query.next()) 
	{
		ui.comboBox->addItem(query.value(0).toString() + "-" + query.value(1).toString());
	}
	lista("%%%");
}

void QCenovnikWidget::lista(const QString& nameSearch)
{
    QLocale loc;
    int mag_id = 1;
    int r = 0;
    int c = 6;
    int fieldNo[6];
    QString country[6];
    int colWidth[] = {50, 200, 200, 50, 100, 80};

    QString temp = "";
    temp = 
		" SELECT  komintenti.id, komintenti.naziv, artikli.artikal, artikli.edm, cenovnik.prodazna_cena, cenovnik.ddv "
		" FROM magacin_prodavnica_cenovnik AS cenovnik "
		" LEFT JOIN artikli ON cenovnik.artikal_id = artikli.id "
		" LEFT JOIN komintenti ON cenovnik.prodavnica_id = komintenti.id "
		" WHERE cenovnik.prodavnica_id = "+nameSearch;
	QSqlQuery query(temp);
    QSqlError err = query.lastError();
    model = new QStandardItemModel(r, c);
	model->setHeaderData( 0, Qt::Horizontal, trUtf8("Ид."));
	model->setHeaderData( 1, Qt::Horizontal, trUtf8("Продавница"));
    model->setHeaderData( 2, Qt::Horizontal, trUtf8("Артикал"));
    model->setHeaderData( 3, Qt::Horizontal, trUtf8("Едм"));
	model->setHeaderData( 4, Qt::Horizontal, trUtf8("Продaжна Цена"));
	model->setHeaderData( 5, Qt::Horizontal, trUtf8("ДДВ"));
    ui.tableView->setModel(model);
    header = new QHeaderView(Qt::Horizontal, this);
	header->setClickable(true);

    ui.tableView->setHorizontalHeader(header);
    header->show();
    int row = 0;
	CHelperClass chc(this);
	float vkVlezKol = 0.f;
	float vkIzlezKol = 0.f;
	float vkSaldo = 0.f;
	setUpdatesEnabled(false);
    while (query.next()) 
    {
        for (int i = 0; i < c; i++)
        {
            QString temp = query.value(i).toString();
            QStandardItem *item = NULL; 

			if (i == 4)
			{
				temp =  loc.toString(query.value(i).toFloat() ,'f', 2);
				item = new QStandardItem(temp);
				item->setTextAlignment(Qt::AlignRight);
			}
			else if (i == 5)
			{
				temp =  loc.toString(query.value(i).toFloat() ,'f', 2);
				item = new QStandardItem(temp);
				item->setTextAlignment(Qt::AlignRight);
			}else
			{
				item = new QStandardItem(temp);
				item->setTextAlignment(Qt::AlignLeft);
				item->setEditable(false);
			}
			ui.tableView->setRowHeight(row, 18);
			ui.tableView->setColumnWidth(i, colWidth[i]);
			model->setItem(row, i, item);
        }
		row++;
    } 
	setUpdatesEnabled(true);

    QItemSelectionModel *sm = ui.tableView->selectionModel();
    connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
        this, SLOT(selectionChanged(QModelIndex,QModelIndex)));
    ui.tableView->show();
}

void QCenovnikWidget::on_lineEdit_textChanged(const QString &)
{
}


void QCenovnikWidget::on_tableView_clicked(const QModelIndex &modelX)
{
	int i = modelX.row();
	m_selectedText = model->item(i, 0) ? model->item(i, 0)->text():"";
}

void QCenovnikWidget::setFocusSearchField()
{
}


void QCenovnikWidget::pressReturn()
{

}

void QCenovnikWidget::setInitText(QString text)
{

}

void QCenovnikWidget::selectionChanged(QModelIndex modelX, QModelIndex modelY)
{
    int i = modelX.row();
    m_selectedText = model->item(i, 0) ? model->item(i, 0)->text():"";
    m_selectedTextName = model->item(i, 1) ? model->item(i, 1)->text():"";
    m_selectedTextNameSaldo = model->item(i, 5) ? model->item(i, 5)->text():"";
}

